package com.baitiaojun.common.utils.validation;


import com.baitiaojun.common.utils.collection.CollectionUtils;

import javax.validation.*;
import java.util.Set;

/**
 * 手动参数校验工具
 */
public class ValidationUtils {

    /**
     * 对参数进行校验, 并返回自定义异常提示
     * @param object
     * @param params
     * @param <T>
     */
    public static <T> void validation(T object, Class<?>... params) {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<T>> validates = validator.validate(object, params);
        if (CollectionUtils.isNotEmpty(validates)) {
            throw new ConstraintViolationException(validates);
        }
    }
}
